import { STORE_NAME } from "../../config/index.js"

export function getStore(name, defVal) {//获取本地数据
    let data = window.sessionStorage.getItem(STORE_NAME + '_' + name)
    if (data) {
        if (typeof data === 'string') {
            try {
                return JSON.parse(data)
            } catch (error) {
                return data
            }
        } else {
            return data
        }
    } else {
        return defVal
    }
}

export function setStore(name, val) {//设置本地数据
    if (typeof val === 'string' || typeof val === 'number') {
        window.sessionStorage.setItem(STORE_NAME + '_' + name, val)
    } else {
        window.sessionStorage.setItem(STORE_NAME + '_' + name, JSON.stringify(val))
    }
}

export function removeStore(name) {//删除本地数据
    window.sessionStorage.removeItem(STORE_NAME + '_' + name)
}